package com.osdp.uc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osdp.common.constant.CommonConstant;
import com.osdp.common.util.BeansUtil;
import com.osdp.common.util.ConvertUtil;
import com.osdp.uc.dto.PositionDTO;
import com.osdp.sc.service.IScRolePositionService;
import com.osdp.uc.util.Fields;
import com.osdp.uc.entity.UcPosition;
import com.osdp.uc.mapper.UcPositionMapper;
import com.osdp.uc.service.IUcPositionService;
import com.osdp.uc.vo.UcPositionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class UcPositionServiceImpl extends ServiceImpl<UcPositionMapper, UcPosition> implements IUcPositionService {

    @Autowired
    private UcPositionMapper ucPositionMapper;

    @Autowired
    private IScRolePositionService ucPositionRoleService;

    @Override
    public IPage<UcPositionVo> pageListByOrgId(UcPositionVo positionVo, Page<UcPosition> page) {
        UcPosition entity = ConvertUtil.sourceToTarget(positionVo,UcPosition.class);
        if(CommonConstant.TREE_ROOT_VALUE.equals(positionVo.getOrgId())){//虚拟节点
            entity.setOrgId(null);
        }
        return this.ucPositionMapper.pageListByOrgId(entity,page)
                .convert(track ->  BeansUtil.copy(track, new UcPositionVo()));
    }

    @Override
    public IPage<UcPositionVo> pageList(UcPositionVo positionVo, Page<UcPosition> page) {
        return super.page(page, new QueryWrapper<UcPosition>()
                .like(StringUtils.isNotBlank(positionVo.getPositionName()), Fields.UcPositionQ.positionName, positionVo.getPositionName())
                .like(StringUtils.isNotBlank(positionVo.getPositionCode()), Fields.UcPositionQ.positionCode, positionVo.getPositionCode())
                .orderByDesc(Fields.UcPositionQ.createTime))
                .convert(track -> BeansUtil.copy(track, new UcPositionVo()));
    }

    @Override
    public IPage<UcPositionVo> selectPositionPageList(UcPositionVo positionVo, Page<UcPosition> page) {
        UcPosition entity = ConvertUtil.sourceToTarget(positionVo,UcPosition.class);
        if(CommonConstant.TREE_ROOT_VALUE.equals(positionVo.getOrgId())){//虚拟节点
            entity.setOrgId(null);
        }
        return this.ucPositionMapper.selectPositionPageList(entity, page)
                .convert(track ->  BeansUtil.copy(track, new UcPositionVo()));
    }

    @Override
    @Transactional
    public boolean insertPosition(UcPositionVo positionVo) {
        UcPosition entity = ConvertUtil.sourceToTarget(positionVo, UcPosition.class);
        boolean isSuccess = super.save(entity);

        //修改与角色的关系
        List<String> roleIds = positionVo.getRoles();
        if(isSuccess && !CollectionUtils.isEmpty(roleIds)) {
            isSuccess = this.ucPositionRoleService.batachSaveOrUpdatePositionRoleByPositionId(entity.getId(), roleIds);
        }
        return isSuccess;
    }

    @Override
    @Transactional
    public boolean updatePosition(UcPositionVo positionVo) {
        UcPosition entity = ConvertUtil.sourceToTarget(positionVo, UcPosition.class);
        entity.setUpdateTime(new Date());
        //修改与角色的关系
        List<String> roleIds = positionVo.getRoles();
        if(!CollectionUtils.isEmpty(roleIds)) {
            this.ucPositionRoleService.batachSaveOrUpdatePositionRoleByPositionId(entity.getId(), roleIds);
        }
        return super.updateById(entity);
    }

    @Override
    public UcPositionVo getByPositionId(String id) {
        UcPosition entity =  super.getById(id);
        return ConvertUtil.sourceToTarget(entity, UcPositionVo.class);
    }

    @Override
    @Transactional
    public boolean deletePosition(String id) {
        this.ucPositionRoleService.deleteByPositionId(id);
        return super.removeById(id);
    }

    @Override
    @Transactional
    public boolean deleteBatcah(List<String> ids) {
        for (String id: ids) {
            this.ucPositionRoleService.deleteByPositionId(id);
        }
        return super.removeByIds(ids);
    }

    @Override
    public List<PositionDTO> queryPositionByUserId(String userId){
        List<UcPosition> list = ucPositionMapper.queryPositionByUserId(userId);
        return ConvertUtil.sourceToTarget(list, PositionDTO.class);
    }

    @Override
    public List<UcPosition> queryPositionByIds(List<String> ids) {
        return ucPositionMapper.queryPositionByIds(ids);
    }
}
